(function () {
    let page = document.querySelector('#page')
    let up = document.querySelector('.up')
    let down = document.querySelector('.down')
    let option = document.querySelectorAll('option')
    let info = document.querySelector('.info')
    console.log(info)

    let back = document.querySelector('.back')

    back.addEventListener('click', function () {
        // console.log(1)
        history.back()
    })


    let index

    console.log(up)
    getValue()
    getInfo(index)
    console.log(index)
    down.onclick = () => {
        // console.log(1)
        up.disabled = false
        index++
        option[index].selected = true
        getInfo(index)
        if (index == 13) {
            down.disabled = true
        }
    }

    up.onclick = () => {
        down.disabled = false

        index--
        option[index].selected = true
        getInfo(index)

        if (index == 0) {
            up.disabled = true
        }

    }

    page.onchange = () => {
        getValue()
        getInfo(index)
        if (index == 0) {
            up.disabled = true
        } else {
            up.disabled = false
        }

        if (index == 13) {
            down.disabled = true
        } else {
            down.disabled = false

        }

    }

    //事件委托给ul
    info.addEventListener('click', function (e) {
        // console.log(e.target.parentNode.parentNode.parentNode.nodeName)

        let li = e.target.parentNode.parentNode.parentNode
        let id = li.getAttribute('productId')
        console.log(id)
        location.href = '../pages/Particulars.html?productId=' + id


    })




    //定义一个函数 去获得option 的value 
    function getValue() {
        //获取到所有的option然后循环找到被选中的哪一个
        for (let i = 0; i < option.length; i++) {
            if (option[i].selected) {
                index = option[i].value
            }


        }

        return index

    }

    //封装请求数据函数
    function getInfo(index) {
        //发起请求
        $.ajax({
            url: `http://chst.vip:1234/api/getmoneyctrl?pageid=${index}`,
            success(res) {
                // console.log(res.result)
                // console.log(res.result)
                let str = ''
                let regExp = /(?<=imgurl=)[^'"]*/
                res.result.forEach(item => {
                    // console.log(item)
                    str += `
                            <li productId=${item.productId}>
                            <a href="javascript:;" productId=${item.productId}>
                            <div class="pic">
                            <img src=${item.productImg2.match(regExp)}>
                                </div>
                                <div class="infoText">
                                    <div class="infoText_Top">
                                        ${item.productName} 
                                        <span class="red">${item.productPinkage}</span>
                                    </div>
                                    <div class="infoText_Bottom">
                                        <span class="info_Left">
                                            ${item.productFrom} | ${item.productTime}
                                        </span>
                                        <span class="info_Right">
                                            <span class="iconfont icon-xinxi"></span>
                                            ${item.productComCount}
                                        </span>
                                    </div>
                                </div>
                            </a>
                        </li>
            
                            `
                })
                $('.info').html(str)

            }
        })

    }
})()